package net.neiquan.neiquandemowithkf.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import net.neiquan.neiquandemowithkf.R;
import net.neiquan.neiquandemowithkf.fragment.TabThreeFragment;
import net.neiquan.neiquandemowithkf.fragment.TabFourFragment;
import net.neiquan.neiquandemowithkf.fragment.TabOneFragment;
import net.neiquan.neiquandemowithkf.fragment.TabTwoFragment;
import net.neiquan.neiquandemowithkf.utils.ResourcesUtils;
import net.neiquanlib.base.BaseActivity;
import net.neiquanlib.widget.FragmentTabHost;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 作者 : hjb
 * 时间 : 2016/4/25.
 */
public class MainActivity extends BaseActivity {
    public static final String TAG = MainActivity.class.getSimpleName();
    @InjectView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @InjectView(R.id.main_tab_host)
    FragmentTabHost mainTabHost;
    @InjectView(R.id.main_tab_wrapper)
    public LinearLayout mMainTabWrapper;
    private Class tabHostFragment[] = {TabOneFragment.class, TabTwoFragment.class, TabThreeFragment.class, TabFourFragment.class};
    private int tabHostImg[] = {R.drawable.tab_1_btn, R.drawable.tab_2_btn, R.drawable.tab_3_btn, R.drawable.tab_4_btn};
    private String[] tabHostNames = ResourcesUtils.getStringArray(R.array.main_tab_host);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        init();
    }

    public void init() {
        mainTabHost.setup(this, getSupportFragmentManager(), R.id.fragment_container);
        for (int i = 0; i < tabHostFragment.length; i++) {
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mainTabHost.newTabSpec(tabHostNames[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            mainTabHost.addTab(tabSpec, tabHostFragment[i], null);
            TabWidget tabWidget = mainTabHost.getTabWidget();
            tabWidget.setGravity(Gravity.CENTER_VERTICAL);
            tabWidget.setDividerDrawable(null);
            //可设置Tab区域的背景
            //mainTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.选择器);
        }
        int showWhichTab = getIntent().getIntExtra(TAG, 0);
        switchFragment(showWhichTab);
    }

    private long clickTime = 0; //记录第一次点击的时间

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!super.onKeyDown(keyCode, event)) {
                if ((System.currentTimeMillis() - clickTime) > 2000) {
                    Toast.makeText(this, "再按一次后退出程序",
                            Toast.LENGTH_SHORT).show();
                    clickTime = System.currentTimeMillis();
                } else {
                    this.finish();
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private View getTabItemView(int index) {
        View view = View.inflate(this, R.layout.main_tab_item, null);
        ImageView tabimg = (ImageView) view.findViewById(R.id.tab_img);
        TextView tabtv = (TextView) view.findViewById(R.id.tab_tv);
        tabimg.setImageResource(tabHostImg[index]);
        tabtv.setText(tabHostNames[index]);
        return view;
    }

    /**
     * 提供给首页,商家,论坛,我的之间相互切换方法
     *
     * @param index
     */
    public void switchFragment(int index) {
        mainTabHost.setCurrentTab(index);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
